home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / V_SLIDER.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  14.7 KB  |  529 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.output.loaded_image;
  4. import sub_arctic.output.drawable;
  5. import java.awt.Color;
  6. import java.util.Vector;
  7.  
  8. /**
  9.  * This the class that knows how to the display-side work of displaying
  10.  * a vertical scrollbar. It handles outputting a scrollbar from a 
  11.  * set of state variables.  
  12.  *
  13.  * @author Scott Hudson
  14.  */
  15. public class v_slider_display extends base_interactor {
  16.  
  17.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  18.  
  19.   /**
  20.    * This variable is a boolean which says if you want a box around the
  21.    * slider or not.
  22.    */
  23.   protected boolean _boxed=true;
  24.  
  25.   /**
  26.    * Return whether or not the user wants a box around the slider or
  27.    * not.
  28.    * @return boolean true if the user wants the slider boxed
  29.    */
  30.   public boolean boxed(){ return _boxed;};
  31.  
  32.   /**
  33.    * Modify whether or not to box the slider.
  34.    * 
  35.    * @param boolean b the new boxed state variable
  36.    */
  37.   public void set_boxed(boolean b) { _boxed=b;
  38.   damage_self();
  39.   }
  40.  
  41.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  42.  
  43.   /* this is here so subclasses can draw the thumb at places other than
  44.    * against the left edge of the interactor. This is a shift in the *X*
  45.    * direction. */
  46.  
  47.   /**
  48.    * This variable is how much to offset (in x and in pixels) the slider's
  49.    * thumb by when drawing it. 
  50.    */
  51.   protected int _thumb_shift=0;
  52.  
  53.   /**
  54.    * Return how much to shift the thumb by (in x, and in pixels).
  55.    * @return int how much to shift the thumb by (in x, and in pixels).
  56.    */
  57.   public int thumb_shift() { return _thumb_shift;}
  58.  
  59.   /**
  60.    * Change the amount to shift the thumb by (in the x direction)
  61.    * @param int v the amount to shift the thumb by in pixels
  62.    */
  63.   public void set_thumb_shift(int v) { 
  64.     _thumb_shift=v;
  65.     damage_self();
  66.   }
  67.  
  68.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  69.   
  70.   /** Image for the up button */
  71.   protected loaded_image _up_img;
  72.   
  73.   /** 
  74.    * Image for the up button 
  75.    * 
  76.    * @return the up image. 
  77.    */
  78.   public loaded_image up_img() {return _up_img;}
  79.   
  80.   /** 
  81.    * Set the image for the up button 
  82.    * 
  83.    * @param loaded_image img the image to use for the up button
  84.    */
  85.   public void set_up_img(loaded_image img) {
  86.     _up_img = img;
  87.     damage_self();
  88.   }
  89.  
  90.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  91.  
  92.   /** Image for the down button */
  93.   protected loaded_image _dn_img;
  94.  
  95.   /** 
  96.    * Image for the down button 
  97.    * 
  98.    * @return the down image. 
  99.    */
  100.   public loaded_image dn_img() {return _dn_img;}
  101.  
  102.   /** 
  103.    * Set the image for the down button 
  104.    * 
  105.    * @param loaded_image img the image to use for the down button
  106.    */
  107.   public void set_dn_img(loaded_image img) {
  108.     _dn_img = img;
  109.     damage_self();
  110.   }
  111.  
  112.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  113.  
  114.   /** Image for the "thumb" */
  115.   protected loaded_image _thumb_img; 
  116.  
  117.   /** 
  118.    * Image for the thumb 
  119.    * 
  120.    * @return the right image. 
  121.    */
  122.   public loaded_image thumb_img() {return _thumb_img;} 
  123.  
  124.   /** 
  125.    * Set the image for the thumb of the slider
  126.    * 
  127.    * @param loaded_image img the image to use for the thumb
  128.    */
  129.   public void set_thumb_img(loaded_image img) {
  130.     _thumb_img = img;
  131.     damage_self();
  132.   }
  133.  
  134.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  135.  
  136.   /** Image for the background -- use null for a default solid color */
  137.   protected loaded_image _back_img;
  138.  
  139.   /** 
  140.    * Image for the background (pattern). You'll get null for a default
  141.    * solid color.
  142.    * 
  143.    * @return the background image
  144.    */
  145.   public loaded_image back_img() {return _back_img;}
  146.  
  147.   /** 
  148.    * Set the image for the background -- use null for a default solid color 
  149.    * 
  150.    * @param loaded_image img the image for the background of the slider
  151.    */
  152.   public void set_back_img(loaded_image img) {
  153.     _back_img = img;
  154.     damage_self();
  155.   }
  156.  
  157.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  158.  
  159.   /** Minimum value of the slider */
  160.   protected int _min_val;
  161.  
  162.   /** Minimum value of the slider
  163.    * @return int the minimum value of the slider  */
  164.   public int min_val() {return _min_val;}
  165.  
  166.   /** 
  167.    * Set the minimum value of the slider 
  168.    * 
  169.    * @param int val the new  minimum value of the slider.
  170.    */
  171.   public void set_min_val(int val) {
  172.     _min_val = val;
  173.     damage_self();
  174.   }
  175.  
  176.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  177.  
  178.   /** Maximum value of the slider */
  179.   protected int _max_val;
  180.  
  181.   /** Maximum value of the slider 
  182.   * @return the maximum value of the slider*/
  183.   public int max_val() {return _max_val;}
  184.  
  185.   /** 
  186.    * Set the maximum value of the slider 
  187.    * 
  188.    * @param int val the new maximum value of the slider.
  189.    */
  190.   public void set_max_val(int val) {
  191.     _max_val = val;
  192.     damage_self();
  193.   }
  194.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  195.  
  196.   /** Small increment value */
  197.   protected int _small_inc;
  198.  
  199.   /** 
  200.    * Return the small increment value (this is the amount the slider's 
  201.    * value changes when you push the end buttons).
  202.    * 
  203.    * @return int the small increment value of the slider
  204.    */
  205.   public int small_inc() {return _small_inc;}
  206.  
  207.   /** 
  208.    * Set the small increment value (amount the value changes when you click on 
  209.    * the end buttons.
  210.    * 
  211.    * @param int val the new small increment 
  212.    */
  213.   public void set_small_inc(int val) {_small_inc = val;}
  214.  
  215.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  216.  
  217.   /** Large increment value */
  218.   protected int _large_inc;
  219.  
  220.   /** 
  221.    * Return the large increment value (this is the amount the slider's 
  222.    * value changes when you click in the thumb area but not on the thumb).
  223.    * 
  224.    * @return int the large increment value of the slider
  225.    */
  226.   public int large_inc() {return _large_inc;}
  227.  
  228.   /** 
  229.    * Set the large increment value (amount the value changes when you 
  230.    * click on the thumb area but not on the thumb).
  231.    * 
  232.    * @param int val the new small increment 
  233.    */
  234.   public void set_large_inc(int val) {_large_inc = val;}
  235.  
  236.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  237.  
  238.   /** 
  239.    * Return the value of the part_a component of this object.  In our case 
  240.    * this is stored in _value. 
  241.    *
  242.    * @return int the value of part_a.
  243.    */
  244.   public int part_a()
  245.     {
  246.       /* Make sure its up to date and in range then return it */
  247.       eval_part_a();
  248.       conform();
  249.       return _value;
  250.     }
  251.  
  252.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  253.  
  254.   /** 
  255.    * Set part_a value directly bypassing the constraint system (but doing 
  256.    * damage).  This should normally only be done by the constraint system 
  257.    * or some other part of the system which takes care of marking things 
  258.    * out-of-date, etc. itself.  In this case part_a is stored in _value.
  259.    *
  260.    * @param int v the new value of part_a.
  261.    */
  262.   protected void set_raw_part_a(int v) 
  263. {
  264.       /* don't do anything unless this is a change */
  265.       if (v != _value)
  266.     {
  267.       /* make change and do damage */
  268.           _value = v;
  269.       conform();
  270.           damage_self();
  271.     }
  272.     }
  273.  
  274.    //had:
  275.    //* @exception general PROPAGATED.
  276.  
  277.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  278.  
  279.   /** 
  280.    * Set the part_a component of this object.  In our case this is stored in
  281.    * _value.  
  282.    * @parm int v the new value.
  283.    */
  284.   public void set_part_a(int v) 
  285. {
  286.       /* if this has a constraint throw an exception */
  287.       if ((active_constraints() & PART_A) != 0)
  288.         throw new sub_arctic_error(
  289.       "Attempt to assign value to constrained part_a " +
  290.       "(AKA h_slider_display.value)");
  291.  
  292.       /* don't do anything unless this is a change */
  293.       if (v != _value)
  294.     {
  295.       set_raw_part_a(v);
  296.       mark_part_a_ood();
  297.     }
  298.     }
  299.  
  300.    //had:
  301.    //* @exception cannot_assign if the value is constrained.
  302.    //* @exception general PROPAGATED.
  303.  
  304.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  305.   
  306.   /** Current value of the slider */
  307.   protected int _value;
  308.   
  309.   /** 
  310.    * Current value of the slider 
  311.    * @return int the value of the slider.
  312.    */
  313.   public int value() {return part_a();}
  314.  
  315.   /** 
  316.    * Set the current value of the slider 
  317.    * @param int val the new value of the slider
  318.    */
  319.   public void set_value(int val) 
  320. {
  321.   set_part_a(val);
  322.     }
  323.  
  324.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  325.  
  326.   /**
  327.    * Full constructor for the v_slider_display.  If you do not supply 
  328.    * an image for any of the image slots (i.e. you pass null) a default
  329.    * look will be applied for you.
  330.    * 
  331.    * @param int          xv       x position of the slider.
  332.    * @param int          yv       y position of the slider.
  333.    * @param int          hv       height of the slider (in pixels).
  334.    * @param int          minv     minimum value of the slider.
  335.    * @param int          maxv     maximum value of the slider.
  336.    * @param int          init_val initial value of the slider.
  337.    * @param int          sincv    small increment value.
  338.    * @param int          lincv    large increment value.
  339.    * @param loaded_image up_im    image for the up button.
  340.    * @param loaded_image dn_im    image for the down button.
  341.    * @param loaded_image th_im    image for the thumb.
  342.    * @param loaded_image back_pat image for the background of the slider.
  343.    */
  344.   public v_slider_display(
  345.     int xv, int yv, int hv, 
  346.     int minv, int maxv, 
  347.     int init_val,
  348.     int sincv, int lincv,
  349.     loaded_image up_im, loaded_image dn_im, loaded_image th_im, 
  350.     loaded_image back_pat)
  351.     {
  352.       /* let the superclass init (with dummy temporary width) */
  353.       super(xv,yv, 21,hv);
  354.  
  355.       /* initialize locals */
  356.       _min_val = minv; _max_val = maxv;
  357.       _value = init_val;
  358.       _small_inc = sincv; _large_inc = lincv;
  359.  
  360.       _up_img = up_im; _dn_img = dn_im; _thumb_img = th_im;
  361.       _back_img = back_pat;
  362.  
  363.       /* use defaults for any nulls */
  364.       if (_up_img == null)   _up_img     = std.v_slider_up();
  365.       if (_dn_img == null)   _dn_img     = std.v_slider_down();
  366.       if (_thumb_img == null) _thumb_img = std.v_slider_thumb();
  367.       if (_back_img == null)  _back_img = std.v_slider_back();
  368.  
  369.       /* now reset the width to the real width */
  370.       int max_w = 0; 
  371.       if (max_w < _up_img.width()) max_w = _up_img.width();
  372.       if (max_w < _dn_img.width()) max_w = _dn_img.width();
  373.       if (max_w < _thumb_img.width()) max_w = _thumb_img.width();
  374.       set_intrinsic_w(max_w);
  375.  
  376.       /* make sure values are all in range and in order */
  377.       conform();
  378.     }
  379.  
  380.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  381.  
  382.   /** 
  383.    * Construct a new v_slider_display with the artkit look.  
  384.    * 
  385.    * @param int xv       x position of the slider.
  386.    * @param int yv       y position of the slider.
  387.    * @param int hv       height of the slider (in pixels).
  388.    * @param int minv     minimum value of the slider.
  389.    * @param int maxv     maximum value of the slider.
  390.    * @param int init_val initial value of the slider.
  391.    * @param int sincv    small increment value. 
  392.    * @param int lincv    large increment value.
  393.    */
  394.   public v_slider_display(
  395.     int xv, int yv, int hv, 
  396.     int minv, int maxv, 
  397.     int init_val,
  398.     int sincv, int lincv)
  399.     {
  400.       this(xv,yv,hv,minv,maxv,init_val,sincv,lincv, null, null, null, null);
  401.     }
  402.  
  403.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  404.  
  405.   /** 
  406.    * Make sure min <= max and min <= value <= max 
  407.    */
  408.   protected void conform()
  409.     {
  410.       int t;
  411.  
  412.       /* make sure min, max are still ok */
  413.       if (_min_val > _max_val)
  414.     {
  415.       t = _min_val; _min_val = _max_val; _max_val = t;
  416.     }
  417.  
  418.       /* force value in range */
  419.       if (_value < _min_val) {_value = _min_val;}
  420.       if (_value > _max_val) {_value = _max_val;}
  421.     }
  422.  
  423.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  424.  
  425.   /** 
  426.    * Indicate that we intrinsically constrain width. Thus, the width
  427.    * is not modifiable by either the programmer or user.
  428.    * @return int bitset indicating intrinsically constrained parts.
  429.    */
  430.   public int intrinsic_constraints()
  431.     {
  432.       return W;
  433.     }
  434.  
  435.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  436.  
  437.   /** 
  438.    * Compute the offset to the thumb in pixels (returns -1 for no thumb)  
  439.    * 
  440.    * @return int the amount the thumb should be shifted down to account for 
  441.    *             the value of the slider.
  442.    */
  443.   protected int thumb_offset()
  444.     {
  445.       int off, slide_range, value_range;
  446.  
  447.       /* compute ranges for thumb */
  448.       slide_range = h() - _up_img.height()-_dn_img.height()-_thumb_img.height();
  449.       value_range = max_val() - min_val();
  450.  
  451.       /* return negative if there is no room for the thumb */
  452.       /*
  453.        * Added Keith's fix for thumbs that are exactly fitting
  454.        */
  455.       if (slide_range < 0 || value_range < 0) return -1;
  456.       /**
  457.        * Added Keith's fix for avoiding a divide by zero.
  458.        */
  459.       if (slide_range == 0 || value_range == 0) return 0;
  460.  
  461.       /* otherwise compute drawing offset for thumb */
  462.       off = (value() - min_val()) * slide_range / value_range;
  463.       if (off < 0) off = 0;
  464.       if (off > slide_range) off = slide_range;
  465.  
  466.       return off;
  467.     }
  468.  
  469.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  470.  
  471.   /** 
  472.    * Draw the current image on the drawable provided.
  473.    *
  474.    * @param drawable d a drawable for the image or screen you want this object 
  475.    *                   drawn on.
  476.    */
  477.   protected void draw_self_local(drawable d) 
  478. {
  479.       int off;
  480.  
  481.       /* fill the rectangle behind the slider */
  482.       if (_back_img != null)
  483.         {
  484.       d.tileImage(_back_img, 0,0, w()-1,h()-1);
  485.     }
  486.  
  487.       /* compute offset for thumb */
  488.       off = thumb_offset();
  489.  
  490.       /* draw the thumb if we have room for one */
  491.       if (off >= 0)
  492.     {
  493.           d.drawImage(_thumb_img, thumb_shift(),_up_img.height()+off);
  494.     }
  495.  
  496.       /* draw the buttons */
  497.       d.drawImage(_up_img,0,0);
  498.       d.drawImage(_dn_img,0,h()-_dn_img.height());
  499.  
  500.       /* if boxed is true, draw a box around the whole thing */
  501.       if (boxed()) {
  502.     d.setColor(Color.black); 
  503.     d.drawRect(0,0, w()-1,h()-1);
  504.       }
  505.     }
  506.  
  507.    //had:
  508.    //* @exception general PROPAGATED.
  509.  
  510.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  511.  
  512. }
  513. /*=========================== COPYRIGHT NOTICE ===========================
  514.  
  515. This file is part of the subArctic user interface toolkit.
  516.  
  517. Copyright (c) 1996 Scott Hudson and Ian Smith
  518. All rights reserved.
  519.  
  520. The subArctic system is freely available for most uses under the terms
  521. and conditions described in 
  522.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  523. and appearing in full in the lib/interactor.java source file.
  524.  
  525. The current release and additional information about this software can be 
  526. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  527.  
  528. ========================================================================*/
  529.